home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / AppsToGo / DTS.Draw / PenSizeDialog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-22  |  4.9 KB  |  194 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        PenSizeDialog.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19.  
  20.  
  21. /*****************************************************************************/
  22.  
  23.  
  24.  
  25. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  26. #include "App.defs.h"        /* Get various application definitions.            */
  27. #include "App.protos.h"        /* Get the prototypes for the application.        */
  28.  
  29. #ifndef __TREEOBJ2__
  30. #include "TreeObj2.h"
  31. #endif
  32.  
  33.  
  34.  
  35. extern short    gPenHeight, gPenWidth, gCtlNum;
  36.  
  37.  
  38.  
  39. /*****************************************************************************/
  40. /*****************************************************************************/
  41.  
  42. #ifdef applec
  43. #pragma segment DTSDrawSeg1
  44. #endif
  45.  
  46. /*****************************************************************************/
  47. /*****************************************************************************/
  48.  
  49.  
  50.  
  51. /* Find out which tool was clicked or double-clicked on. */
  52.  
  53. static Boolean    PenSizeFilter(TEHandle teHndl, EventRecord *event, short *handled);
  54. static Boolean    PenSizeFilter(TEHandle teHndl, EventRecord *event, short *handled)
  55. {
  56. #ifndef __MWERKS__
  57. #pragma unused (teHndl, handled)
  58. #endif
  59.  
  60.     char    key;
  61.     short    i;
  62.  
  63.     key = event->message   & charCodeMask;
  64.     i   = event->modifiers & keyCodeMask;
  65.     if (i & cmdKey)                    return(false);
  66.     if (i & optionKey)                return(false);
  67.     if (key == 8)                    return(false);
  68.     if (key == 9)                    return(false);
  69.     if ((key >= 28) && (key <= 31)) return(false);
  70.  
  71.     if ((key < '0') || (key > '9')) return(true);
  72.  
  73.     return(false);
  74. }
  75.  
  76. OSErr    PENSInitContent(FileRecHndl frHndl, WindowPtr window)
  77. {
  78.     OSErr            err;
  79.     TreeObjHndl        root, cobj;
  80.     ControlHandle    ctl;
  81.     TEHandle        te;
  82.     Handle            txt;
  83.     short            penHeight, penWidth, ph, pw, i;
  84.     Boolean            gotSize;
  85.     Str15            pstr;
  86.  
  87.     err = AddControlSet(window, (*frHndl)->fileState.sfType, kwStandardVis, 0, 0, nil);
  88.     if (err) return(err);
  89.  
  90.     penHeight = gPenHeight;        /* Assume that there are none selected. */
  91.     penWidth  = gPenWidth;
  92.     gotSize   = false;
  93.  
  94.     frHndl = GetNextDocument(window, kDocFileType);
  95.     root   = (*frHndl)->d.doc.root;
  96.     for (i = (*root)->numChildren; i;) {
  97.         cobj = GetChildHndl(root, --i);
  98.         if (DoTreeObjMethod(cobj, GETSELECTMESSAGE, 0)) {
  99.             if ((*cobj)->type < GROUPOBJ) {
  100.                 ph = mDerefCommon(cobj)->penHeight;
  101.                 pw = mDerefCommon(cobj)->penWidth;
  102.                 if (!gotSize) {
  103.                     gotSize = true;
  104.                     penHeight = ph;
  105.                     penWidth  = pw;
  106.                     continue;
  107.                 }
  108.                 if (penHeight != ph) penHeight = -1;
  109.                 if (penWidth  != pw) penWidth  = -1;
  110.             }
  111.         }
  112.     }
  113.  
  114.     pstr[0] = 0;
  115.     if (!penHeight)
  116.         ++penHeight;
  117.     if (penHeight > -1)
  118.         pcpydec(pstr, penHeight);
  119.     txt = NewHandle(pstr[0]);
  120.     BlockMove(pstr + 1, *txt, pstr[0]);
  121.     CNum2Ctl(window, 100, &ctl);
  122.     te = (TEHandle)GetControlReference(ctl);
  123.     UseControlStyle(ctl);
  124.     DisposeHandle(CTESwapText(te, txt, nil, true));
  125.     TESetSelect(0, 32767, te);
  126.     CTESetKeyFilter(te, PenSizeFilter);
  127.  
  128.     pstr[0] = 0;
  129.     if (!penWidth)
  130.         ++penWidth;
  131.     if (penWidth > -1)
  132.         pcpydec(pstr, penWidth);
  133.     txt = NewHandle(pstr[0]);
  134.     BlockMove(pstr + 1, *txt, pstr[0]);
  135.     CNum2Ctl(window, 101, &ctl);
  136.     te = (TEHandle)GetControlReference(ctl);
  137.     DisposeHandle(CTESwapText(te, txt, nil, true));
  138.     TESetSelect(0, 32767, te);
  139.     CTESetKeyFilter(te, PenSizeFilter);
  140.  
  141.     return(err);
  142. }
  143.  
  144.  
  145.  
  146. /*****************************************************************************/
  147.  
  148.  
  149.  
  150. OSErr    PENSFreeWindow(FileRecHndl frHndl, WindowPtr window)
  151. {
  152.     ControlHandle    ctl;
  153.     TEHandle        te;
  154.     Str15            pstr;
  155.     short            i;
  156.     TreeObjHndl        root, cobj;
  157.     OSErr            err;
  158.  
  159.     if (gCtlNum == -1) {
  160.         CNum2Ctl(window, 100, &ctl);
  161.         te = (TEHandle)GetControlReference(ctl);
  162.         BlockMove(*((*te)->hText), pstr + 1, (pstr[0] = (*te)->teLength));
  163.         gPenHeight = p2dec(pstr, nil);
  164.         if (!gPenHeight)
  165.             ++gPenHeight;
  166.         CNum2Ctl(window, 101, &ctl);
  167.         te = (TEHandle)GetControlReference(ctl);
  168.         BlockMove(*((*te)->hText), pstr + 1, (pstr[0] = (*te)->teLength));
  169.         gPenWidth = p2dec(pstr, nil);
  170.         if (!gPenWidth)
  171.             ++gPenWidth;
  172.         frHndl = GetNextDocument(window, kDocFileType);
  173.         root   = (*frHndl)->d.doc.root;
  174.         for (i = (*root)->numChildren; i;) {
  175.             cobj = GetChildHndl(root, --i);
  176.             if (DoTreeObjMethod(cobj, GETSELECTMESSAGE, 0)) {
  177.                 if ((*cobj)->type < GROUPOBJ) {
  178.                     err = ModifyChild(PENSIZE_EDIT, root, i, false);
  179.                     if (err) return(err);
  180.                     mDerefCommon(cobj)->penHeight = gPenHeight;
  181.                     mDerefCommon(cobj)->penWidth  = gPenWidth;
  182.                 }
  183.             }
  184.         }
  185.         HideWindow(window);
  186.         DoImageDocument(frHndl);
  187.     }
  188.  
  189.     return(noErr);
  190. }
  191.  
  192.  
  193.  
  194.